package com.wushi.lenovo.asia5b.app_di.wish.presenter;

import com.mvp.base.BasePresenter;
import com.mvp.base.NetParams;
import com.mvp.common.GlobalValue;
import com.mvp.common.LocalManageUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_di.wish.mvpview.WishFragmentView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.result.WishResModel;

import java.util.Map;

import javax.inject.Inject;

/**
 * Created by zhangyuncai on 2018/12/14.
 */
public class WishFragmentPresenter extends BasePresenter<WishFragmentView> {

    ApiService apiService;
    protected int page;//当前页数
    protected int sumPage;//最后页数
//    private int page;
//    private int sumPage;


    @Inject
    public WishFragmentPresenter(ApiService apiService) {
        this.apiService = apiService;
    }

    public void getWishList(int refresh) {
        if (!UserRepository.isLogin()) {
            return;
        }
        if (refresh <= 1) {
            page = 1;
            sumPage = 1000;
        } else {
            page++;
        }
        if (page > sumPage) {
            getView().dismissLoadingBar("");
            return;
        }
        Map<String, Object> map = MapHelper.newHashMap();
        map.put("member_id", "" + UserInfo.getMemberId());
        map.put("language_code", "" + LocalManageUtil.getAppLanguage());
        map.put("length", "" + GlobalValue.records);
        map.put("page_no", "" + page);
        int country_id = UserInfo.getCountryId();
        if (country_id <= 0) {
            country_id = 4;
        }
        map.put("country_id", "" + country_id);
        //country_id
        baseSubscribe(apiService.getWishList(map)
                , new NetParams(false)
                , resultModel -> {
                    if (resultModel.isSuccess() && resultModel.getData() != null) {
                        WishResModel wishResModel = resultModel.getData();
                        if (wishResModel != null) {
                            sumPage = GlobalValue.getSumPage(GlobalValue.records, wishResModel.getTotal());
                            if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                                getView().getWishListResult(wishResModel.getData(), page, sumPage, wishResModel.getTotal());
                            }
                        }
                    }
                });
    }

    public void deleteWish(int prod_id) {
        if (!UserRepository.isLogin()) {
            return;
        }
        baseSubscribe(apiService.deleteWish(UserInfo.getMemberId(), prod_id + "")
                , resultModel -> {
                    if (UserRepository.isLogin()) {//无人登录的情况下仍然显示数据
                        getView().deleteWishResult(resultModel, prod_id);
                    }
                });
    }

}